import Vue from 'vue'
import axios from 'axios'
import {
  Loading,
  Message
} from 'element-ui';
import serverConfig from '../serverConfig/serverApi'
import qs from 'qs'
// 响应时间
// axios.defaults.timeout = 5 * 1000
// 配置cookie
axios.defaults.withCredentials = true
// 配置请求头
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 静态资源
// Vue.prototype.$static = ''

// 配置接口地址
// http://49.235.218.198:8080/xgwj-platform
// http://47.110.33.164:8080
//'http://118.190.104.193:8080/xgwj-platform'
//http://47.105.124.23:8080/xgwj-platform
//121.199.63.66:8080/xgwj-platform
axios.defaults.baseURL = 'http://121.199.63.66:8080/xgwj-platform'
// 把axios配置到VUE构造函数上, 目的是 为了能够让所有的组件都可以调用axios
Vue.prototype.$http = axios

let loadingInstance = '';
// POST传参序列化(添加请求拦截器)
// axios.interceptors.request.use(
//   config => {
//     // loadingInstance = Indicator.open({
//     //     text: '加载中...',
//     //     spinnerType: 'fading-circle'
//     //   });
//     if (config.method === 'post') {
//       config.data = qs.stringify(config.data)
//     }
//     return config
//   },
//   err => {
//     // loadingInstance.close()
//     Toast({
//         message: '请求失败',
//         position: 'bottom',
//         duration: 3000
//       });
//     return Promise.reject(err)
//   }
// )
// 返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
  res => {
    if (res.status === 200) {
      if (!res.data.success) {
        Message({
          message: res.data.resultMsg,
          type: 'warning',
          duration: 3000,
        });
      }
      return res
    } else {
      Message({
        message: res.data.msg,
        type: 'warning',
        duration: 3000,
      });
    }
  },
  err => {
    Message({
      message: '请求失败，请稍后再试',
      type: 'error',
      duration: 3000,
    });
    return Promise.reject(err)
  }
)
// 发送请求
export function fetchPost(url, params ,loading) {
  if(loading){
    loadingInstance = Loading.service()
  }
  return new Promise((resolve, reject) => {
    axios
      .post(serverConfig[url], params)
      .then(
        res => {
          if(res.data.success && loading){
            loadingInstance.close();
          }
          resolve(res.data)
        }
      )
      .catch(err => {
        if(loading){
          loadingInstance.close();
        }
        reject(err.data)
      })
  })
}
export function fetchGet(url, params,loading) {
  if(loading){
    loadingInstance = Loading.service()
  }
  return new Promise((resolve, reject) => {
    axios
      .get(serverConfig[url], {
        params: params
      })
      .then(res => {
        if(res.data.success && loading){
          loadingInstance.close();
        }
        resolve(res.data)
      })
      .catch(err => {
        if(loading){
          loadingInstance.close();
        }
        reject(err.data)
      })
  })
}
export function fetchDelete(url, params,loading) {
  if(loading){
    loadingInstance = Loading.service()
  }
  return new Promise((resolve, reject) => {
    axios
      .delete(serverConfig[url], {
        params: params
      })
      .then(res => {
        if(res.data.success && loading){
          loadingInstance.close();
        }
        resolve(res.data)
      })
      .catch(err => {
        if(loading){
          loadingInstance.close();
        }
        reject(err.data)
      })
  })
}
/*  组件内部的调用方法 */
// getData () {
//   let params = {
//     userId: this.userId
//   }
//   this.$get('/xxx', params).then(res => {
//     this.listsData = res.data
//   }).catch(() => {
//   })
// }